package com.yhos.platform.oopston.core.utils;

import org.apache.commons.lang3.time.FastDateFormat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
    public static String FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_NOT_TIME = "yyyy-MM-dd";
    public static String FORMAT_LONG = "yyyyMMddHHmmssms";
    public static String FORMAT_SHORT = "yyMMdd";
    public static String YEAR_MONTH = "yyyy-MM";

    public DateUtils() {
    }

    public static String getNow(Date date) {
        return FastDateFormat.getInstance(FORMAT).format(date);
    }

    public static String getTimeStamp(Date date) {
        return String.valueOf(date.getTime());
    }

    public static String getLongFormat(Date date) {
        return FastDateFormat.getInstance(FORMAT_LONG).format(date);
    }

    public static String getShortFormat(Date date) {
        return FastDateFormat.getInstance(FORMAT_LONG).format(date);
    }

    public static Date stringToDate(String str) {
        return stringToDate(str, FORMAT_NOT_TIME);
    }

    public static Date stringToDate(String str, String dateFormat) {
        DateFormat format = new SimpleDateFormat(dateFormat);
        Date date = null;

        try {
            date = format.parse(str);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return date;
    }

    public static String dateToString(Date dateVal) {
        return dateToString(dateVal, FORMAT);
    }

    public static String dateToString(Date dateVal, String format) {
        if(dateVal != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(dateVal);
        } else {
            return "";
        }
    }

    public static String getDateByCalendar(Calendar calendar, String format) {
        return FastDateFormat.getInstance(format).format(calendar);
    }

    public static String getPrevMonths(String format, Integer prevMons) {
        Calendar calendar = Calendar.getInstance();
        StringBuffer result = new StringBuffer(getDateByCalendar(calendar, format));

        for(int i = 0; i < prevMons.intValue(); ++i) {
            calendar.add(2, -1);
            result.append("," + getDateByCalendar(calendar, format));
        }

        return result.substring(0, result.length());
    }

    public static Date add(Date date, Integer type, Integer day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(type.intValue(), day.intValue());
        return calendar.getTime();
    }

    /*
    * 将时间字符串转换为时间戳字符串
    */
    public static String dateStringToTimeStampString(String dateParam, String format) throws ParseException{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = simpleDateFormat.parse(dateParam);
        long timeStamp = date.getTime();
        return String.valueOf(timeStamp);
    }

   /*
   * 将时间戳字符串转换为时间字符串
   */
    public static String timeStampStringToDateString(String timeStampParam, String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        long longTime = new Long(timeStampParam);
        Date date = new Date(longTime);
        return simpleDateFormat.format(date);
    }

    /*
  * 将时间戳字符串转换为时间类型
  */
    public static Date timeStampStringToDate(String timeStampParam){
        long longTime = new Long(timeStampParam);
        Date date = new Date(longTime);
        return date;
    }

    /**
     * 日期转日期时间 2018-12-05 => 2018-12-05 23:59:59
     * @param date
     * @return
     */
    public static Date convertToEndDateTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        try {
            Date endDateTime = sdf.parse(sdf.format(date));
            cal.setTime(endDateTime);
            cal.add(Calendar.DATE, 1);
            cal.add(Calendar.SECOND, -1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cal.getTime();
    }

    public static void main (String [] args) throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyM");
        Date date5 = simpleDateFormat.parse("20185");
    }
}
